use std::thread;
use std::sync::{Arc, Barrier, Mutex,Once};
use std::sync::mpsc;
use futures::executor::block_on;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;
static INIT:Once = Once::new();
fn main() {
    let data = Arc::new(Mutex::new(vec![1,2,3]));
    let barrier = Arc::new(Barrier::new(2));
    let mut thread_list = Vec::new();
    let a = 12;
    let (tx, rx) = mpsc::sync_channel(0);    
    for i in 0..3{
        let data = Arc::clone(&data);
        let tx = tx.clone();        
        let index = i;
        let handle = thread::spawn(move ||{   
            println!("begin");
            let message = i.to_string() + "hello";         
            tx.send(message);    
            INIT.call_once(||{                            
                let s = thread::current().id();
                println!("the init by thread is {:?}",s);
            });

            let s = thread::current().id();
            println!("the thread is {:?}",s);
            let mut nums = data.lock().unwrap();
            let  num = nums.get_mut(index).unwrap();
            *num += 2;
        });

        thread_list.push(handle);
    }
    drop(tx);
    for send in rx{
        println!("send: {}", send);
    }
    for handle in thread_list{
        handle.join().unwrap();
    }

    let s = thread::current().id();
    println!("the thread is {}",a);
    println!("the vec is {:?}",data.clone().lock().unwrap());

}